from django.shortcuts import render
import hashlib

from Trade.models import User
from Trade.models import Goods

"""
加密密码，算法单次md5
:param password: 传入的密码
:return: 加密后的密码
"""


def setPassword(password):
    md5 = hashlib.md5()
    md5.update(password.encode())
    password = md5.hexdigest()
    return str(password)


def indexShow(request):
    request.session.flush()
    return render(request, 'main.html')


def indexShowLater(request):
    # userNow = request.session.is_empty()
    # user = request.session.get('userName')
    # ussss = request.session.get('userName')
    return render(request, 'main.html')


def userInfoShow(request):
    return render(request, 'userInfo.html', {'name': request.session.get('userName')})


def loginView(request):
    return render(request, 'login.html')


# 登录按钮提交后，验证数据库中是否存在用户,存在就跳转主网页
def loginViewSubmit(request):
    u = request.POST.get('userName', '')
    p = request.POST.get('userPassword', '')
    if u and p:
        user = User.objects.filter(userName=u, userPassword=setPassword(p)).count()
        if user >= 1:
            request.session['userName'] = u
            request.session.set_expiry(0)
            return render(request, 'userInfo.html', {'name': u})
        else:
            return render(request, 'login.html')
    else:
        return render(request, 'login.html')


# 注册页面
def registerView(request):
    return render(request, 'register.html')


# 注册用户，将用户信息存进数据库
def registerViewSubmit(request):
    u = request.POST.get('userName', '')
    p = request.POST.get('userPassword', '')
    if u and p:
        cnt = User.objects.filter(userName=u).count()
        if cnt >= 1:
            return render(request, 'registerFailed.html')
        else:
            stu = User(userName=u, userPassword=setPassword(p), userType=1)
            stu.save()
            return render(request, 'registerSuccessful.html')
    else:
        return render(request, 'registerFailed.html')


def registerSuccessful(request):
    return render(request, 'login.html')


def addGoods(request):
    n = request.POST.get('name', '')
    p = request.POST.get('price', '')
    t = request.POST.get('type', '')
    o = request.POST.get('owner', '')
    d = request.POST.get('description', '')
    if n and p and t and o and d:
        goods = Goods(name=n, price=p, type=t, owner=o, description=d)
        goods.save()
        return render(request, 'insertOk.html')
    return render(request, 'userInfo.html', {'name': request.session.get('userName', '')})


def deleteGoods(request):
    n = request.POST.get('name', '')
    o = request.POST.get('owner', '')
    if n and o:
        cnt = Goods.objects.filter(name=n, owner=o).count()
        if cnt >= 1:
            Goods.objects.filter(name=n, owner=o).delete()
        else:
            return render(request, 'takedownFail.html')
    else:
        return render(request, 'takedownFail.html')
    return render(request, 'takedown.html')


def maintop(request):
    user = request.session.is_empty()
    return render(request, 'top.html', {'user': user})


def mainleft(request):
    return render(request, 'left.html')


def selectGoods(request):
    goods = Goods.objects.all()
    return render(request, 'goods.html', {'goods': goods})


def search(request):
    n = request.POST.get('name', '')
    if n:
        goods = Goods.objects.filter(name=n)
    else:
        return render(request, 'goods.html')
    return render(request, 'goods.html', {'result': goods})


def clothes(request):
    goods = Goods.objects.filter(type='clothes')
    return render(request, 'goods.html', {'clothes': goods})


def electronic(request):
    goods = Goods.objects.filter(type='electronic')
    return render(request, 'goods.html', {'electronic': goods})


def other(request):
    goods = Goods.objects.filter(type='other')
    return render(request, 'goods.html', {'other': goods})


def book(request):
    goods = Goods.objects.filter(type='book')
    return render(request, 'goods.html', {'book': goods})


def editView(request):
    return render(request, 'editPassword.html')


def editViewSubmit(request):
    u = request.session.get('userName', '')
    op = request.POST.get('oddPassword', '')
    np = request.POST.get('newPassword', '')
    if u and op and np:
        user = User.objects.filter(userName=u, userPassword=setPassword(op)).count()
        if user >= 1:
            User.objects.filter(userName=u, userPassword=setPassword(op)).delete()
            stu = User(userName=u, userPassword=setPassword(np), userType=1)
            stu.save()
            return render(request, 'userInfo.html', {'name': u})
        else:
            return render(request, 'editPassword.html')
    else:
        return render(request, 'editPassword.html')


def editSuccessful(request):
    return render(request, 'userInfo.html', {'name': request.session.get('userName', '')})


def logoutView(request):
    request.session.flush()
    return render(request, 'main.html')
